<?php

namespace app\common\library\wechat;

/**
 * 微信小程序用户管理类
 * Class WxUser
 * @package app\common\library\wechat
 */
class WxUser extends WxBase
{
    /**
     * 获取session_key
     * @param $code
     * @return array|mixed
     */
    public function sessionKey($code)
    {
        /**
         * code 换取 session_key
         * ​这是一个 HTTPS 接口，开发者服务器使用登录凭证 code 获取 session_key 和 openid。
         * 其中 session_key 是对用户数据进行加密签名的密钥。为了自身应用安全，session_key 不应该在网络上传输。
         */
        $url = 'https://api.weixin.qq.com/sns/jscode2session';
        $result = json_decode(stripslashes(curl($url, [
            'appid' => $this->appId,
            'secret' => $this->appSecret,
            'grant_type' => 'authorization_code',
            'js_code' => $code
        ])), true);
        if (isset($result['errcode'])) {
            $this->error = $result['errmsg'];
            return false;
        }
        return $result;
    }

    public function accessToken(){
        if (!isset($_SESSION['access_token']) || (isset($_SESSION['expires_in']) && time() > $_SESSION['expires_in'])){
            //access_token过期
            $url = 'https://api.weixin.qq.com/cgi-bin/token';
            $result = json_decode(curl($url, [
                'appid' => $this->appId,
                'secret' => $this->appSecret,
                'grant_type' => 'client_credential'
            ]), true);
            $access_token = $result['access_token'];
            $_SESSION['access_token'] = $access_token;
            $_SESSION['expires_in'] = time() + 7200;
        }else{
            $access_token = $_SESSION['access_token'];
        }
        return $access_token;
    }

    public function getPhoneNumber($accessToken,$code){
        $url = 'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token='.$accessToken;
        $result = json_decode(curlPost($url, json_encode([
            'code' => $code
        ],true)), true);
        if (isset($result['errcode']) && $result['errcode'] != 0) {
            $this->error = $result['errmsg'];
            return false;
        }
        return $result;
    }

}